﻿#include "GraphicsScene.h"
#include <QGraphicsSceneMouseEvent>
#include <QDebug>


GraphicsScene::GraphicsScene(QObject* parent) : QGraphicsScene(parent)
{
    isDrawStatus = false;
}

void GraphicsScene::startCreate(DefineItemType itemType)
{
    drawType = itemType;
    isDrawStatus = true;
    pointList.clear();
}

void GraphicsScene::cancelCreate()
{
    isDrawStatus = false;
    pointList.clear();
}

void GraphicsScene::finishCreate()
{
    emit sigCreateFinished();
    cancelCreate();
}

void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
    if(isDrawStatus){
        QPointF p(event->scenePos().x(), event->scenePos().y());
        if(event->buttons() == Qt::LeftButton){
            pointList.push_back(p);
            emit sigPointPush(p, pointList.size() - 1);
            switch(drawType){
            case DIT_Rect:
            case DIT_Circle:
            case DIT_Line:
            {
                if (pointList.size() == 2) {
                    finishCreate();
                }
            }
            break;
            case DIT_RectR:
            case DIT_CircleC:
            case DIT_Caliper:
            {
                if (pointList.size() == 3) {
                    finishCreate();
                }
            }
            break;
            case DIT_Point:
            {
                if (pointList.size() == 1) {
                    finishCreate();
                }
            }
            break;
            }
        }
    } else {
        QGraphicsScene::mousePressEvent(event);
    }
}

void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if(isDrawStatus){
        QPointF p(event->scenePos().x(), event->scenePos().y());
        switch(drawType){
        case DIT_Polygon:
        {
            if (pointList.size() == 1) {
                pointList.push_back(p);
                emit sigPointPush(p, pointList.size() - 1);
            }
            if(pointList.size() > 0){
                emit sigPointMove(p);
            }
        }
            break;
        case DIT_Rect:
        case DIT_RectR:
        case DIT_Circle:
        case DIT_CircleC:
        case DIT_Caliper:
        case DIT_Line:
        {
            if(pointList.size() > 0){
                emit sigPointMove(p);
            }
        }
            break;
        case DIT_Point:
            break;
        default:
            break;
        }
    } else {
        QGraphicsScene::mouseMoveEvent(event);
    }
}

void GraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if(isDrawStatus){
        QPointF p(event->scenePos().x(), event->scenePos().y());
        switch(drawType){
        case DIT_Polygon:
        {
            if (pointList.size() >= 3) {
                // emit sigPointPush(pointList.first(), pointList.size() - 1);
                emit sigCreateFinished();
                isDrawStatus = false;
                pointList.clear();
            } else {
                for(int i = pointList.size(); i < 3; ++i){
                    pointList.push_back(p);
                    emit sigPointPush(p, pointList.size() - 1);
                }
                emit sigCreateFinished();
                isDrawStatus = false;
                pointList.clear();
            }
        }
        break;
        }
    } else {
        QGraphicsScene::mouseDoubleClickEvent(event);
    }
}
